<template>
  <div>
    <router-view />
    <el-dialog title="运检" :visible="globalDialog" append-to-body :show-close="false">
      <h5>故障电池编号: {{ current_stoke.cell + 1 }}#</h5> 
      <h5>故障类型: {{ current_stoke.key1 }}</h5>
      <h5>故障等级: {{ current_stoke.level }}</h5>
      <h5>故障表现: {{ current_stoke.key2 }}, {{ current_stoke.msg }}</h5>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onGoto">
          查看运检策略
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "App",
  data() {
    return {
      cell_pre: 0,
      group_pre: 0,
      pcs_pre: 0,
      gap: 300,
      step: 30,
      preLisen: null
    }
  },
  computed: {
    ...mapGetters(["globalDialog", "isLogin", "msg", 'current_stoke']),
  },
  methods: {
    onClose() {
      this.$store.commit('globalDialog', false);
      this.$store.commit('noDialog');

      // setTimeout(() => {
      //   this.$store.commit('noDialog', false);
      // }, 1000 * 10 * 60)
    },
    onGoto() {
      this.$store.commit('globalDialog', false);
      this.$router.push({
        path: '/yunjian'
      })
    },
    onStartListen() {
      clearInterval(this.preLisen);
      this.preLisen = setInterval(()=>{
        if(this.isLogin){
          
          // set cell data
          let cell_data = []
          let all_cell_data = window.allData.cells
          let max_length = 0

          all_cell_data.forEach(cell => {
            let sub_data = {}
            for(let k in cell){
              sub_data[k] = cell[k].slice(this.cell_pre, this.cell_pre + this.gap)
              max_length = cell[k].length
            }
            cell_data.push(sub_data)
          })
          this.cell_pre = this.cell_pre + this.step
          this.$store.commit('cell_data', cell_data)

          if ( this.cell_pre + this.gap >= max_length ){
            this.cell_pre = 0
          }

          // set cell_group 
          let cell_group = []
          let all_cell_group = window.allData.cell_groups
          
          all_cell_group.forEach(group => {
            let sub_group = {}
            for(let k in group){
              sub_group[k] = group[k].slice(this.group_pre, this.group_pre + this.gap)
              max_length = group[k].length
            }
            cell_group.push(sub_group)
          })
          this.group_pre = this.group_pre + this.step
          this.$store.commit('cell_group', cell_group)
           if ( this.group_pre + this.gap > max_length ){
            this.group_pre = 0
          }
          // console.log(this.group_pre)
          // console.log(cell_data[0]['告警'])
          // set pcs
          let pcs = []
          let all_pcs = window.allData.PCS
          
          all_pcs.forEach(pc => {
            let sub_pc = {}
            for(let k in pc){
              sub_pc[k] = pc[k].slice(this.pcs_pre, this.pcs_pre + this.gap)
              max_length = pc[k].length
            }
            pcs.push(sub_pc)
          })
          this.pcs_pre = this.pcs_pre + this.step
          this.$store.commit('pcs_data', pcs)

          if (this.pcs_pre + this.gap > max_length){
            this.pcs_pre = 0
          }

         
          // console.log(this.preIdx)
    
          cell_data.forEach((cell, index) => {
            let cell_h = cell['预警'][this.gap - 1]
            // console.log(cell_h)
            
              
              if(Object.keys(cell_h).length > 0){
             
                // if(cell_h['等级'] >= 3){
                  this.$store.commit('current', {cell: index, level: cell_h['等级'],   key1: cell_h['类型'], key2: cell_h['子类型'], msg: cell_h['描述']})
                  this.$store.commit('set_msg', `电池：${index + 1} 出现${cell_h['类型']}${cell_h['子类型']}故障`)
                  // break
                // }
              
            }
            
          })

          // console.log('start listen')
          // this.$store.commit('globalDialog', true);
        }
        // this.$store.commit('current', {key1: '电压故障', key2: '电压过高(DYGG)'})
        
      }, 1000)
    }
  },
  mounted() {
    this.onStartListen()
  }
};
</script>

<style>
</style>
